package com.bnyy.medicalHousekeeper.moudle.message;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;

import java.io.File;
import java.io.IOException;

import static android.media.MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2022/6/30
 **/
public class AudioManager {

    private static final String TAG = "AudioManager";

    private static AudioManager audioManager;

    private AudioManager() {
    }

    public static AudioManager getInstance() {
        if (audioManager == null) {
            audioManager = new AudioManager();
        }
        return audioManager;
    }

    private MediaPlayer mediaPlayer;
    private String playingPath = "";
    private Listener l;
    private boolean preparing = false;
    private MediaRecorder mediaRecorder;

    public void play(String path, Listener listener) {
        if (preparing) {
            return;
        }
        stop();

        l = listener;
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {
                return false;
            }
        });
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                stop();
                return false;
            }
        });
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                AudioManager.this.playingPath = path;
                preparing = false;
                if (l != null)
                    l.onPrepared();
                mediaPlayer.start();
            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                stop();
            }
        });
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepareAsync();
            preparing = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void record(Context context) {
        File parent = new File(context.getExternalFilesDir(null), "/Audio");
        if (!parent.exists()) {
            parent.mkdir();
        }
        File file = new File(parent, System.currentTimeMillis() + ".amr");

        mediaRecorder = new MediaRecorder();
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setMaxDuration(MessageManager.MAX_AUDIO_DURATION);
        mediaRecorder.setOutputFile(file);
        mediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
            @Override
            public void onInfo(MediaRecorder mr, int what, int extra) {
                if (what == MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
                    recordFinish();
                }
            }
        });
        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void recordFinish() {
        if (mediaRecorder != null) {
            try {
                mediaRecorder.stop();
                mediaRecorder.release();
                mediaRecorder = null;
            } catch (Exception e) {
                mediaRecorder = null;
                e.printStackTrace();
            }
        }
    }

    /**
     *
     * @param path
     * @return 时长，单位毫秒
     */
    public int getDuration(String path) {
        int duration = 0;

        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare();
            duration = mediaPlayer.getDuration();
            mediaPlayer.release();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return duration;
    }

    public void pause() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }
        }
    }

    public void stop() {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.release();
            mediaPlayer = null;
            this.playingPath = "";
        }
        preparing = false;
        if (l != null) {
            l.onStop();
            l = null;
        }
    }

    public String getPlayingPath() {
        return playingPath;
    }

    public interface Listener {
        void onPrepared();

        void onStop();
    }
}
